import 'dart:async';

import 'package:cron/cron.dart';

class Task {
  Cron? _cronTimer;
  ScheduledTask? _scheduledTask;

  start(String cronFormat) async {
    await close();
    _cronTimer = Cron();
    Timer.run(() async {
      print("first run at ${DateTime.now().toString()}");
      await Future.delayed(Duration(seconds: 30));
    });
    _scheduledTask = _cronTimer?.schedule(Schedule.parse(cronFormat), () async {
      print("run at ${DateTime.now().toString()}");
      await Future.delayed(Duration(seconds: 30));
    });
  }

  close() async {
    await _scheduledTask?.cancel();
    await _cronTimer?.close();
  }
}

main() {
  var task = Task();
  task.start("*/1 * * * *");
}
